<?php
	
	/**
	 * 转帐
	 * @param int $pTransferType;//转账类型 1确认转账 2 取消转账
	 * @param int $deal_id  标的id	 
	 * @param string $ref_data 逗号分割的, 1：投资,填还款日期(int)  ; 2代偿，3代偿还款列表; 4债权转让: id; 5结算担保收益:金额，如果为0,则取fanwe_deal.guarantor_pro_fit_amt ;
	 * @param int $MerCode  商户ID
	 * @param string $cert_md5 
	 * @param string $post_url
	 * @return string
	 */
	function DoBidsOne($deal_load_id,$platformNo,$ws_url){ 
	
		set_time_limit(0);
		$results = array();
		$results['status'] = 1;
		
		/*
		$pWebUrl= SITE_DOMAIN.APP_ROOT."/index.php?ctl=collocation&act=response&class_name=".getCollName()."&class_act=DoBidsOne&from=".$_REQUEST['from']."&repay_start_time=".$repay_start_time;//web方式返回
		$pS2SUrl= SITE_DOMAIN.APP_ROOT."/index.php?ctl=collocation&act=notify&class_name=".getCollName()."&class_act=DoBidsOne&from=".$_REQUEST['from']."&repay_start_time=".$repay_start_time;//s2s方式返回
		*/
		
		$deal_load = $GLOBALS['db']->getRow("select fcp.*,dorder.money,dorder.deal_id,dorder.user_id,dorder.ecv_id,dorder.interestrate_id from ".DB_PREFIX."fuioupay_cp_transaction as fcp left join ".DB_PREFIX."deal_load as dorder on dorder.pP2PBillNo=fcp.requestNo where fcp.code = '0000' and dorder.id = ".$deal_load_id." and fcp.is_complete_transaction = 0");
		
		require_once(APP_ROOT_PATH."system/libs/user.php");
		require_once(APP_ROOT_PATH.'system/collocation/fuioupay/transferBu.php');
		
		$tuser = get_user_info("*","id=".$deal_load['user_id']);
		
		$data = $Sign_data= array();
		$requestNo = $deal_load["requestNo"]; 
		$data['requestNo'] = $requestNo;//请求流水号
		$data['platformNo'] = $platformNo;// 商户编号
		
		$Sign_data['ver'] = "0.44";
		$Sign_data['contract_no']=$deal_load["contract_no"]; 
		$Sign_data['in_cust_no']=$deal_load["in_cust_no"]; 
		$Sign_data['mchnt_cd']=$platformNo; 
		$Sign_data['mchnt_txn_ssn']=get_microtime();
		$Sign_data['out_cust_no']=$deal_load["out_cust_no"];
		   
		$Sign_data['rem']='退款';
		
		ksort($Sign_data);
		
		$Sign_old = '';
		foreach($Sign_data as $k => $v){
			if($k == 'ver'){
				$Sign_old .=$v;
				$Sign_old_a .=$k;
			}else{
				$Sign_old .= $v."|";
				$Sign_old_a .= $k."+|+";
			}
		}
		
		/* 签名数据 */
		$pSign=rsaSign($Sign_old,PRIVATE_KEY_PATH);

		$data['mode'] = "CANCEL";	
		
		/* 请求参数 */  
		$req = http_build_query($Sign_data)."&signature=".urlencode($pSign);

		$fuioupay_log = array();
		$fuioupay_log['code'] = 'DOBidsOne';
		$fuioupay_log['create_date'] = to_date(TIME_UTC,'Y-m-d H:i:s');
		$fuioupay_log['strxml'] = $req;
		$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$fuioupay_log);
		$log_id = $GLOBALS['db']->insert_id();

		/* 调用账户查询服务 */
	//if(!file_exists(APP_ROOT_PATH.'system/collocation/fuioupay/resuls.php')){
		$options = array(
			CURLOPT_POST => TRUE,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_SSL_VERIFYPEER=>0,
			CURLOPT_SSL_VERIFYHOST=>0,
			CURLOPT_POSTFIELDS =>$req,
		
		); 
		$ch = curl_init($ws_url); 
		curl_setopt_array($ch,$options); 
		$resultStr = curl_exec($ch);
		curl_close($ch); 
		
	//}else{
	//	$resultStr = file_get_contents(APP_ROOT_PATH.'system/collocation/fuioupay/resuls.php');	
	//	$ch = 1;
	//}
		$result = array();
		if (empty($resultStr)){
			$result = array();
			$result['resp_code'] = 9999;
			$result['message'] = '返回出错';
			//print_r(htmlentities($result));echo "<hr/>";exit;
		}else{
			$GLOBALS['db']->query("update ".DB_PREFIX."fuioupay_cp_transaction set is_callback = 2 ,update_time=".TIME_UTC." where is_callback = 1 and requestNo = ".$requestNo);
			require_once(APP_ROOT_PATH.'system/collocation/fuioupay/xml.php');
			$str3ParaInfo = @XML_unserialize($resultStr);
			$str3Req = $str3ParaInfo['ap']['plain'];
			$result = array();
			$result['resp_code'] = $str3Req["resp_code"];
			$result['mchnt_cd'] = $str3Req["mchnt_cd"];
			$result['mchnt_txn_ssn'] = $str3Req["mchnt_txn_ssn"];
			$result['signature'] =  $str3ParaInfo['ap']['signature']; 
			
			$back_info = array('html'=>$str);
			$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$back_info,'UPDATE',"id = ".$log_id);
			
			$results['resp_code'] = $str3Req["resp_code"];
			
			if($str3Req["resp_code"] =='0000')
			{
				if($data['mode']=='CANCEL'){
					//表示取消转账
					$sql = "update ".DB_PREFIX."fuioupay_cp_transaction set is_complete_transaction = 2,update_time=".TIME_UTC." where is_callback = 2 and requestNo = ".$requestNo;
				}
				
				$GLOBALS['db']->query($sql);
				$deal_load = array();
				
				if($data['mode']=='CANCEL'){	
					$deal_load['is_repay'] = 1;//表示取消转账	
				}

				$where = " pP2PBillNo = ".$requestNo;
				
				$load_info = array();
				
				$load_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."deal_load where id ='".$deal_load_id."'");
				
				require_once(APP_ROOT_PATH."system/deal_func.php");
				$deal_info = get_deal($load_info['deal_id']);
				require_once(APP_ROOT_PATH."system/libs/user.php");
				
				//余额返还
				$ecv_money = 0;
				if($load_info['ecv_id'] > 0){
					//$ecv_money = $GLOBALS['db']->getOne("SELECT `money` FROM ".DB_PREFIX."ecv WHERE id=".$load_info['ecv_id']);
					$ecv_money = 0 ;
				}
				//流标返还少了判断红包大于投资金额。后期再看
				if(($load_info['money']- $load_info['learn_money'])-$ecv_money > 0){
					/*if($load_info['is_old_loan']==0)
						modify_account(array("money"=>($load_info['money']-$ecv_money - $load_info['learn_money']),"lock_money"=>-($load_info['money']-$ecv_money- $load_info['learn_money'])),$load_info['user_id'],"[<a href='".$deal_info['url']."' target='_blank'>".$deal_info['name']."</a>],撤销返还",55,1);
					else
						modify_account(array("money"=>($load_info['money']-$ecv_money - $load_info['learn_money'])),$load_info['user_id'],"[<a href='".$deal_info['url']."' target='_blank'>".$deal_info['name']."</a>],撤销返还",55,1);*/
				}
				
				//红包返还
				if($load_info['ecv_id'] > 0)
					$GLOBALS['db']->query("UPDATE ".DB_PREFIX."ecv SET use_count=use_count - 1 WHERE id=".$load_info['ecv_id']);
	
				//加息券返还
				if($load_info['interestrate_id'] > 0)
					$GLOBALS['db']->query("UPDATE ".DB_PREFIX."interestrate SET use_count=use_count - 1 WHERE id=".$load_info['interestrate_id']);
					
				//删除投标记录
				$GLOBALS['db']->query("delete from ".DB_PREFIX."deal_load WHERE id=".$load_info['id']);
				

				//更新标的状态
				$GLOBALS['db']->query("update ".DB_PREFIX."deal set deal_status = 1,load_money=load_money - ".$load_info['money']." where id=".$load_info['deal_id']);

				syn_deal_status($load_info['deal_id']);
				
				$results['message'] = "操作成功";
							
			}else{
				$fuiou_code = fuiou_code();
				$results['message'] = $str =  "resp_code:".$fuiou_code[$result['resp_code']].",pErrMsg:失败";
				$error_data = array('html'=>$str);
				$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$error_data,'UPDATE',"id = ".$log_id);
				
			}
		
		}

		return $results;
	}
	
?>